#include "cPut.h"

#include <stdlib.h>
#include <sstream>
#include <stdio.h>

#include <Poco/DOM/Document.h>
#include <Poco/DOM/NodeIterator.h>
#include <Poco/Base64Encoder.h>
#include <Poco/Base64Decoder.h>
#include <Poco/StreamCopier.h>
#include "misc/Interface.h"
#include "misc/Debug.h"
#include "client/Client.h"
/*********************************************************/
void cPut::execute()
{
	// Clients only sent put messages!
}
/*********************************************************/
std::string cPut::serialize() const
{
	Client& c = Client::Instance();
	std::ostringstream		xml;
	std::ostringstream		filestr;
	id = c.getID();

	// We encode the file name in Base64 so we need to make the filename a 6 bit multiple
	std::string file_ = file;
	while ( ((file_.length()*8) % 6) != 0 ) {
		file_.append("\n");
	}

	// Encode the aligned command
	Poco::Base64Encoder	encoder(filestr);
	encoder << file_;

	// Build the xml string for this job
	xml << "<GS id='" << id << "'>";
	xml << "<Put>";
	xml << "<file>" << filestr.str() << "</file>";
	xml << "<host>" << host << "</host>";
	xml << "</Put>";
	xml << "</GS>";

	return( xml.str() );
}
/*********************************************************/
void cPut::deserialize(Poco::XML::NodeIterator& it)
{
	// Clients only sent put messages!
}
/*********************************************************/
std::string cPut::getHost() const
{
	return host;
}
/*********************************************************/
void cPut::setHost(const std::string& host)
{
	cPut::host = host;
}
/*********************************************************/
std::string cPut::getFile() const
{
	return( file );
}
/*********************************************************/
void cPut::setFile(const std::string& file)
{
	cPut::file = file;
}
